package com.material.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.material.common.annotation.LoginUser;
import com.material.common.base.CommonController;
import com.material.common.core.domain.entity.SysUser;
import com.material.common.entity.ResultResponse;
import com.material.common.utils.StringUtils;
import com.material.common.utils.bean.BeanUtil;
import com.material.pound.consts.PoundState;
import com.material.pound.domain.FuelPound;
import com.material.pound.params.FuelPoundQueryParams;
import com.material.pound.params.FuelPoundSettleParams;
import com.material.pound.service.IFuelPoundService;
import com.material.pound.vo.FuelPoundDetailVo;
import com.material.pound.vo.FuelPoundListVo;
import com.material.pound.vo.FuelPoundSettleVo;
import com.material.request.FuelPoundQueryRequest;
import com.material.response.FuelPoundResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author ganxinge
 */
@Api(tags = "9.磅单信息")
@RestController
@RequestMapping("/pound")
public class FuelPoundController extends CommonController {

    @Autowired
    private IFuelPoundService fuelPoundService;

    @ApiOperation("磅单列表")
    @GetMapping("/list")
    public ResultResponse list(@ApiIgnore @LoginUser SysUser sysUser, FuelPoundQueryRequest fuelPoundQueryRequest){
        //checkSupplierUser(sysUser);
        //FuelPound query = new FuelPound();

        FuelPoundQueryParams query = new FuelPoundQueryParams();
        query.setCarNum(fuelPoundQueryRequest.getCarNum());
        query.setStartDate(fuelPoundQueryRequest.getStartDate());
        query.setEndDate(fuelPoundQueryRequest.getEndDate());
        query.setSupplierName(fuelPoundQueryRequest.getSupplierName());

        String userType = sysUser.getUserType();
        if(SysUser.USER_TYPE_3.equals(userType)){
            query.setSupplierId(sysUser.getBizId());
        }else{
            query.setFactoryId(sysUser.getBizId());
        }
        if(StringUtils.isNotBlank(fuelPoundQueryRequest.getState())){
            String[] split = fuelPoundQueryRequest.getState().split(",");
            List<String> strings = Arrays.asList(split);
            query.setStateList(strings);
        }
        PageHelper.startPage(fuelPoundQueryRequest.getPageNum(),fuelPoundQueryRequest.getPageSize(),"id desc");
        List<FuelPoundListVo> fuelPoundListVos = fuelPoundService.selectFuelPoundPageList(query);
        List<FuelPoundResponse> fuelPoundResponses = convertData(fuelPoundListVos);
        PageInfo<FuelPoundResponse> pageInfo = new PageInfo<>(fuelPoundResponses);
        return ResultResponse.success(pageInfo);
    }

    private List<FuelPoundResponse> convertData(List<FuelPoundListVo> fuelPounds){
        if(CollectionUtils.isEmpty(fuelPounds)){
            return new ArrayList<>();
        }
        List<FuelPoundResponse> dataList = new ArrayList<>();
        fuelPounds.forEach(f->{
            FuelPoundResponse fuelPoundResponse = BeanUtil.copyProperties(f, FuelPoundResponse.class);
            Integer state = f.getState();
            String status = "";
            switch (state){
                case 0:
                    status = "待过磅";
                    break;
                case 1:
                    status = "待入库";
                    break;
                case 2:
                    status = "待回皮";
                    break;
                case 3:
                    status = "待质检";
                    break;
                case 4:
                    status = "待化验";
                    break;
                case 5:
                    status = "已化验";
                    break;
                case 6:
                    status = "待结算";
                    break;
                case 7:
                    status = "已结算";
                    break;
            }
            fuelPoundResponse.setStatus(status);
            dataList.add(fuelPoundResponse);
        });
        return dataList;
    }

    @ApiOperation("磅单详情")
    @GetMapping("/detail/{id}")
    public ResultResponse detail(@PathVariable("id") Long id){
        FuelPoundDetailVo fuelPoundDetailVo = fuelPoundService.selectFuelById(id);
        return ResultResponse.success(fuelPoundDetailVo);
    }

    @ApiOperation("结算分析")
    @GetMapping("/settleInfo")
    public ResultResponse settleInfo(@ApiIgnore @LoginUser SysUser sysUser,FuelPoundSettleParams query){
        String userType = sysUser.getUserType();
        if(SysUser.USER_TYPE_3.equals(userType)){
            query.setSupplierId(sysUser.getBizId());
        }else{
            query.setFactoryId(sysUser.getBizId());
        }
        FuelPoundSettleVo fuelPoundSettleVo = fuelPoundService.selectFuelSettleInfo(query);
        if(fuelPoundSettleVo == null){
            return ResultResponse.success();
        }
        return ResultResponse.success(fuelPoundSettleVo);
    }


}
